// pages/message/message.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    responseInterceptor: getApp().responseInterceptor,
    page: 1,
    size: 5,
    isLast: false,
    isLogin: false,
    showDialog: false,
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      page: 1,
      isLogin: getApp().globalData.isLogin
    })
    if (!getApp().globalData.isLogin) {
      this.setData({
        showDialog: true,
        list: []
      })
    } else {
      this.setData({
        list: [],
        showDialog: false
      })
      this.getList();
    }
  },

  /**
 * 页面上拉触底事件的处理函数
 */
  onReachBottom: function () {
    //触底加载下一页
    if (!this.data.isLast && getApp().globalData.isLogin) {
      this.getList();
    }
  },


  //向后端请求数据
  getList() {
    let that = this;
    wx.showLoading({
      title: '加载中',
    });
    // 提交数据
    wx.request({
      url: getApp().globalData.BASE_URL + "/message/user",
      data: {
        userId: getApp().globalData.userInfo.userId,
        page: that.data.page,
        size: that.data.size
      },
      header: {
        'content-type': 'application/json',
        'Authorization': getApp().globalData.TOKEN
      },
      success(res) {
        res = that.data.responseInterceptor(res)
        // console.log(res)
        if(res === undefined) return;
        
        //合并数据
        let result = [...that.data.list, ...res.data.content]
        that.setData({
          list: result,
          page: that.data.page + 1,
          isLast: res.data.last
        })
        wx.hideLoading();
      }

    })
  },

  ChangeReadStatus(e){
    // console.log(e.target.dataset.index)
    let index = e.target.dataset.index
    // console.log(this.data.list[index])
    let list = this.data.list
    let that = this;
    wx.showLoading({
      title: '加载中',
    });
    // 提交数据
    wx.request({
      url: getApp().globalData.BASE_URL + "/message/read?messageId=" + list[index].messageId,
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'Authorization': getApp().globalData.TOKEN
      },
      success(res) {
        if(res.data.code == 200){
          list[index] = res.data.data
          that.setData({
            list: list
          })
          wx.showToast({
            title: res.data.msg,
            duration: 1000
          });
        }
        wx.hideLoading();
      }

    })
  },

  toShowDialog() {
    if (!this.data.isLogin) {
      this.setData({
        showDialog: true
      });
    }
  },
  onClose() {
    this.setData({
      showDialog: false
    });
  },
  onConfirm() {
    this.setData({
      showDialog: false
    });
    wx.navigateTo({
      url: '../login/index/index',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})